package com.cyl.h5.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.h5.config.SecurityUtil;
import com.cyl.manager.ums.domain.entity.Member;
import com.cyl.manager.ums.service.MemberService;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.framework.config.LocalDataUtil;
import com.ruoyi.livesass.api.domain.LbsGksc;
import com.ruoyi.livesass.api.domain.LbsGkscDay;
import com.ruoyi.livesass.api.mapper.LbsGkscDayMapper;
import com.ruoyi.livesass.api.mapper.LbsGkscMapper;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
* @ClassName: H5LiveController 
* @Description: TODO(H5直播相关数据调用) 
* @author 晏大兵 
* @date 2025年3月3日 下午5:54:13 
*
 */
@RestController
@RequestMapping("/h5/live")
@Slf4j
public class H5LiveController {
	
    @Autowired
    private LbsGkscMapper lbsGkscMapper;
    
    @Autowired
    private LbsGkscDayMapper lbsGkscDayMapper;

	@Autowired
	private MemberService memberService;
	  

	    
    public static Long getSpreaduid() {
        Member member = (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        int rolezd = member.getRolezd().intValue();//0->普通用户；1->核销员；2->团长 3 副店长 4 群主
        if(rolezd == 2 || rolezd == 3 || rolezd == 1) {
        	return null;
        }
    	return member.getId();
    }
	    
	    
	/**
	* @Title: gksclist 
	* @Description: TODO(获取观看时长 昨天-周-月) 
	* @param @param order
	* @param @param page
	* @param @return    设定文件 
	* @return ResponseEntity<Page<LbsGksc>>    返回类型 
	* @throws
	 */
    @PostMapping("/ums/member/gksc/list")
    public ResponseEntity<Page<LbsGksc>> gksclist(long memberId,Long spreaduid, String order, Pageable page) {
        Member member = (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        Long deptDpId = member.getDeptDpId();
        
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        
        QueryWrapper<LbsGksc> query = new QueryWrapper<>();
        query.eq("del_flag", 0);
        query.eq("dept_dp_id", deptDpId);
        
        if(memberId != 0 ) {
        	query.eq("wc_user_id", memberId);
        }
        if(spreaduid==null) {
    		spreaduid = getSpreaduid();
    	}
        if(spreaduid != null) {
        	query.eq("spreaduid", spreaduid);
        }

        
        query.orderByDesc(order);
        List<LbsGksc> list = lbsGkscMapper.selectList(query);
        
        for(LbsGksc item : list) {
        	Member memberItem = memberService.selectById(item.getWcUserId());
        	item.setMember(memberItem);
        }
        
        
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }
    
    
	/**
	* @Title: today 
	* @Description: TODO(根据会员ID获取观看时长，日期倒序) 
	* @param @param order
	* @param @param page
	* @param @return    设定文件 
	* @return ResponseEntity<Page<LbsGksc>>    返回类型 
	* @throws
	 */
    @GetMapping("/ums/member/gksc/today")
    public ResponseEntity<Page<LbsGkscDay>> today(long memberId, Pageable page) {
        checkMember(memberId);
    	
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }

        QueryWrapper<LbsGkscDay> query = new QueryWrapper<>();
        query.eq("wc_user_id", memberId);
        query.orderByDesc("gsrq");
        List<LbsGkscDay> list = lbsGkscDayMapper.selectList(query);
        
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }
    
    
    /**
    * @Title: todayList 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param @param deptId
    * @param @param page
    * @param @return    设定文件 
    * @return ResponseEntity<Page<LbsGkscDay>>    返回类型 
    * @throws
     */
    @GetMapping("/ums/member/gksc/todayList")
    public ResponseEntity<Page<LbsGkscDay>> todayList(String gsrq, boolean isAsc, long zbjid, long memberId,Long spreaduid, Pageable page) {
    	Member member = (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        Long deptDpId = member.getDeptDpId();
         
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        

        QueryWrapper<LbsGkscDay> query = new QueryWrapper<>();
        query.eq("dept_dp_id", deptDpId);
        if(!StrUtil.isBlankIfStr(gsrq)) {
        	query.eq("gsrq", gsrq);
        }else {
        	query.eq("gsrq", DateTime.now().toDateStr());
        }
        
        
        if(0 != zbjid) {
            query.eq("zbjid", zbjid);
        }
        
        if(0 != memberId) {
        	query.eq("wc_user_id", memberId);
        }
        
    	
    	if(spreaduid==null) {
    		spreaduid = getSpreaduid();
    	}
    	
        if(spreaduid != null) {
        	query.eq("spreaduid", spreaduid);
        }
        
        if(isAsc) {
        	query.orderByAsc("livetime");
        }else {
        	query.orderByDesc("livetime");
        }
        
        
        List<LbsGkscDay> list = lbsGkscDayMapper.selectList(query);
        
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }
    
    
    
    public void checkMember(Long memberId) {
    	if(SecurityUtil.getLocalMember().getDeptDpId().longValue() != memberService.selectById(memberId).getDeptDpId().longValue()) {
            throw new RuntimeException("不能操作非自己店铺客户数据！");
    	}
    }
    
    
    
    
}
